package vip.xiaonuo.smzq.modular.lifecycle.vo.datamanager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import vip.xiaonuo.smzq.modular.lifecycle.constant.LifeCycleConstant;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 相邻族谱节点vo
 */
@Data
public class AdJoinPedNodeVO {
    @ApiModelProperty("前一个族谱节点")
    private LifeCyclePedigeeNodeVO prePedNode;
    @ApiModelProperty("下一个族谱节点")
    private LifeCyclePedigeeNodeVO nextPedNode;

    /**
     * 初始化族谱节点vo
     * @param lcpns 矿区所有节点列表
     * @param  currDate 当前日期
     */
    public void initAdJoin(List<LifeCyclePedigeeNodeVO> lcpns, LocalDate currDate,boolean isDiscrete){
//        过滤掉压覆节点
        lcpns = lcpns.stream().filter(ele-> !StrUtil.equals(ele.getNodeName(), CollUtil.getFirst(LifeCycleConstant.crePedNodTypes))).collect(Collectors.toList());
        //当前族谱节点vo
        LifeCyclePedigeeNodeVO currPedNodeVO = new LifeCyclePedigeeNodeVO();

        currPedNodeVO.setNodeDate(currDate);

        List<LifeCyclePedigeeNodeVO> sortLcpns = this.getCurrDateSortPedNodes(lcpns,currPedNodeVO);

        Integer curIndex = getCurrDateIndex(sortLcpns,currDate);
//        当前日期索引
        if(curIndex != null){
            //        如果不是最后一个,并且不是第一个
            if(curIndex!=CollUtil.size(sortLcpns)-1 && curIndex!=0){
//                如果是总共有两个
                if(CollUtil.size(sortLcpns)==2){
                    if(!isDiscrete){
                        this.nextPedNode = CollUtil.getLast(sortLcpns);
                        if(ObjectUtil.equals(nextPedNode.getNodeDate(),currDate)){
                            this.nextPedNode = null;
                        }

                    }

                }else{
                    this.prePedNode = CollUtil.get(sortLcpns,curIndex-1);
                    if(!isDiscrete){
                        this.nextPedNode = CollUtil.get(sortLcpns,curIndex+1);
                        if(ObjectUtil.equals(nextPedNode.getNodeDate(),currDate)){
                            this.nextPedNode = null;
                        }
                    }

                }
            }else if(curIndex == 0){
                if(CollUtil.size(sortLcpns)>1){
                    if(!isDiscrete){
                        this.nextPedNode = CollUtil.get(sortLcpns,curIndex+1);
                        if(ObjectUtil.equals(nextPedNode.getNodeDate(),currDate)){
                            this.nextPedNode = null;
                        }
                    }
                }
//                如果是最后一个
            }else if(curIndex==CollUtil.size(sortLcpns)-1){
                this.prePedNode = CollUtil.get(sortLcpns,curIndex-1);
            }

        }


    }



    /**
     * 得到当前日期在已经排好序的族谱列表中的索引
     * @param sortedLcpns 已经排好序的族谱节点列表
     * @param currDate 当前日期
     * @return 索引
     */
    public Integer getCurrDateIndex(List<LifeCyclePedigeeNodeVO> sortedLcpns, LocalDate currDate){
        Integer result = null;
        for (int i = 0; i < CollUtil.size(sortedLcpns); i++) {
            LifeCyclePedigeeNodeVO lcpn = CollUtil.get(sortedLcpns,i);

            if(ObjectUtil.equals(currDate,lcpn.getNodeDate())){
                result = i;
                break;
            }
        }

        return result;

    }

    /**
     * 得到当前矿区已有的族谱节点和当前族谱节点已排好序的族谱节点列表
     * @param lcpns 当前矿区族谱节点
     * @param currPedNodeVO 当前族谱节点
     * @return
     */
    public List<LifeCyclePedigeeNodeVO> getCurrDateSortPedNodes(List<LifeCyclePedigeeNodeVO> lcpns, LifeCyclePedigeeNodeVO currPedNodeVO){
        if(CollUtil.isNotEmpty(lcpns)){
            List<LifeCyclePedigeeNodeVO> fLcpns = lcpns.stream().filter(ele->ObjectUtil.equals(ele.getNodeDate(),currPedNodeVO.getNodeDate())).collect(Collectors.toList());
            if(CollUtil.isEmpty(fLcpns)){
                lcpns.add(currPedNodeVO);
            }
        }else{
            lcpns.add(currPedNodeVO);
        }



        List<LifeCyclePedigeeNodeVO> sortLcpns = CollUtil.sortByProperty(lcpns,"nodeDate");

        return sortLcpns;
    }
}
